#include "vecindad.h"

Vecindad::Vecindad(Imagen * img, QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);
	imagen = img;
    ui.xIn->setMaximum(imagen->getWidth());
    ui.yIn->setMaximum(imagen->getHeight());
	scene = new QGraphicsScene();
	scene->addPixmap(QPixmap::fromImage(imagen->getImagen()));
	view = new QGraphicsView(scene);
	ui.layout->addWidget(view);

}

Vecindad::~Vecindad()
{
	delete scene;
	delete view;
}

void Vecindad::actualizar(){
    QImage seleccion = imagen->getVecindad(ui.xIn->value(), ui.yIn->value(), ui.distancia->value());
	scene->clear();
    int minx = ui.xIn->value() - ui.distancia->value();
    int miny = ui.yIn->value() - ui.distancia->value();
	minx = (minx < 0 )?0:minx;
	miny = (miny < 0 )?0:miny;
	QSize tam(RECTSIZE, RECTSIZE);
	QRgb color;
	QGraphicsTextItem * text;
	for(int i = 0; i < seleccion.height(); i++){
		for(int j = 0; j < seleccion.width(); j++){
			QRect rect(QPoint(j*RECTSIZE, i*RECTSIZE), tam);
			color = seleccion.pixel(j,i);
			scene->addRect(rect, QPen(), QBrush(QColor(color)));
			text = scene->addText(tr("[%1,%2]").arg(minx + j).arg(miny + i));
                        text->setPos(QPointF(j*RECTSIZE, i*RECTSIZE));
			text->setZValue(1);
			text = scene->addText(tr("(%1,%2,%4)").arg(qRed(color)).arg(qGreen(color)).arg(qBlue(color)));
			text->setPos(QPointF(j*RECTSIZE, i*RECTSIZE + RECTSIZE2));
			text->setZValue(1);
		}
	}

}
